/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.cli.propbased;

import gstruct.cli.CLINoArgOption;
import gstruct.cli.ParseResult;

import java.util.Iterator;
import java.util.Properties;

/**
 * CLINoArgOption implementation that stores its results
 * in a {@link Properties} object
 * @author Giulio Franco <giulio_franco@msn.com>
 */
public class CLIPropNoArgOption extends CLINoArgOption<Properties> {
	private final String yesValue;
	private final String noValue;
	
	/**
	 * Creates a CLIPropNoArgOption
	 * @param name	Option name
	 * @param description	Option description
	 * @param yesValue	Value that will be stored if the option is inserted
	 * @param noValue	Value that will be stored if the option is not inserted
	 */
	public CLIPropNoArgOption(String name, String description,
			String yesValue, String noValue) {
		super(name, description, false, false);
		this.yesValue = yesValue;
		this.noValue = noValue;
	}

	/**
	 * Creates a CLIPropNoArgOption.
	 * The option will use null as its noValue.
	 * @param name	Option name
	 * @param description	Option description
	 * @param yesValue	Value that will be stored if the option is inserted
	 */
	public CLIPropNoArgOption(String name, String description,
			String yesValue) {
		this(name, description, yesValue, null);
	}
	
	/**
	 * Creates a CLIPropNoArgOption.
	 * The option will use the empty string as its yesValue
	 * and null as its noValue.
	 * @param name	Option name
	 * @param description	Option description
	 */
	public CLIPropNoArgOption(String name, String description) {
		this(name, description, "", null);
	}

	@Override
	public boolean inserted(String matcher, Iterator<String> args,
			ParseResult<? extends Properties> result) {
		if (super.inserted(matcher, args, result)) {
			result.getResult().setProperty(getKey(),
					(yesValue == null) ? getName() : yesValue);
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean isOk(ParseResult<? extends Properties> result) {
		if (super.isOk(result)) {
			if (noValue != null && !result.getArguments().contains(this)) {
				result.getResult().setProperty(getKey(), noValue);
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Gets the key that will be used to store
	 * the option value in the resulting Properties
	 * @return	Option key
	 */
	protected String getKey() {
		return getName().substring(1);
	}
}
